﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Uber.Core.Model;
using Uber.Core.Repository;

namespace Uber.Data
{
    public class FillPayRequestRepo : BaseRepo, IFillPayRequestRepo
    {
        public FillPayRequestRepo(IConnectionFactory connFactory) : base(connFactory)
        {
        }

        public int Fill(long id, string transactionId)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "fillPayRequest";
                    cmd.Parameters.Add("id", SqlDbType.BigInt).Value = id;
                    cmd.Parameters.Add("transactionId", SqlDbType.NVarChar, 20).Value = transactionId;
                    conn.Open();

                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public IEnumerable<FillPayRequest> GetPage(int page, int pageSize, bool? filled, long? accountId)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getFillPayRequests";
                    cmd.Parameters.Add("pageSize", SqlDbType.Int).Value = pageSize;
                    cmd.Parameters.Add("page", SqlDbType.Int).Value = page;
                    cmd.Parameters.Add("filled", SqlDbType.Bit).Value = filled;
                    cmd.Parameters.Add("accountId", SqlDbType.BigInt).Value = accountId;

                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new FillPayRequest();
                            o.Id = dr.GetInt64(0);
                            o.Name = dr.GetString(1);
                            o.Gold = dr.GetDecimal(2);
                            o.Amount = dr.GetInt32(3);
                            o.Email = dr.GetString(4);
                            o.Timp = dr.GetDateTime(5);
                            o.TransactionId = dr.GetString(6);
                            o.Filled = dr.GetBoolean(7);
                            yield return o;
                        }
                    }
                }
            }
        }

        public FillPayRequest Get(long id)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getFillPayRequest";
                    cmd.Parameters.Add("id", SqlDbType.BigInt).Value = id;
                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new FillPayRequest();
                            o.Id = dr.GetInt64(0);
                            o.Name = dr.GetString(1);
                            o.Gold = dr.GetDecimal(2);
                            o.Amount = dr.GetInt32(3);
                            o.Email = dr.GetString(4);
                            o.Timp = dr.GetDateTime(5);
                            o.TransactionId = dr.GetString(6);
                            o.Filled = dr.GetBoolean(7);
                            return o;
                        }
                    }
                }
            }
            return null;
        }


        public int Count(bool? filled, long? accountId)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getFillPayRequestsCount";
                    cmd.Parameters.Add("filled", SqlDbType.Bit).Value = filled;
                    cmd.Parameters.Add("accountid", SqlDbType.BigInt).Value = accountId;

                    conn.Open();

                    return (int)cmd.ExecuteScalar();
                }
            }
        }
    }
}